package com.jiaoyou.baojian.activity;

import android.os.Bundle;
import android.text.TextUtils;
import android.widget.Button;
import android.widget.EditText;

import com.jiaoyou.baojian.R;
import com.jiaoyou.baojian.http.Api;
import com.jiaoyou.baojian.http.BaofangCallback;

import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.OnClick;
import retrofit2.Call;

public class ModifyPasswordActivity extends BaseActivity {

    @BindView(R.id.oldpassword)
    EditText oldpassword;
    @BindView(R.id.newpassword)
    EditText newpassword;
    @BindView(R.id.newpassword2)
    EditText newpassword2;
    @BindView(R.id.ok)
    Button ok;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        ButterKnife.bind(this);
    }

    @Override
    public int getContentView() {
        return R.layout.activity_modify_password;
    }

    @OnClick(R.id.ok)
    public void onViewClicked() {
        String old = oldpassword.getEditableText().toString();
        String newp = newpassword.getEditableText().toString();
        String newp2 = newpassword2.getEditableText().toString();
        if(TextUtils.isEmpty(old)){
            toast("旧密码不能为空");
            return;
        }
        if(TextUtils.isEmpty(newp)){
            toast("新密码不能为空");
            return;
        }
        if(TextUtils.isEmpty(newp2)){
            toast("新密码不能为空");
            return;
        }
        if(!newp.equals(newp2)){
            toast("新密码两次输入不一致");
            return;
        }
        if(newp.length() < 6){
            toast("密码至少六位");
            return;
        }
        waiting("修改密码...");
        Api.getInstance().changePassword(old, newp,
                new BaofangCallback() {
                    @Override
                    public void onFailure(Call call, Throwable t) {
                        stopWaiting();
                        toast("修改出错：" + t.getLocalizedMessage());
                    }

                    @Override
                    public void onSuccess(Object o) {
                        stopWaiting();
                        toast("修改成功");
                        finish();
                    }
                });
    }
}
